package my2Cents;

import net.phys2d.math.ROVector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.SlideJoint;
import net.phys2d.raw.SpringJoint;

public class Winch extends SpringJoint {
	private static final long serialVersionUID = 1884690177215828333L;
	private int keyForward = -1;
	private int keyReverse = -1;
	private float expandSpeed = 0;
	private float retractSpeed = 0;
	private boolean rigid = false;
	private float length = 0;
	
	public Winch(final Body b1, final Body b2, final ROVector2f anchor1,
			final ROVector2f anchor2) {
		super(b1, b2, anchor1, anchor2);
		setCompressedSpringConst(0);
		setStretchedSpringConst(0);
		setBrokenSpringConst(10000);
	}
	
	public void think() {
		float p = 0;
		if(Sandbox.keyIsDown(keyForward))
			p = expandSpeed;
		
		if(Sandbox.keyIsDown(keyReverse))
			p = -retractSpeed;
		
		if(p == 0) return;
		if(length + p < 1) {
			length = 1;
			p = 0;
		}
		setLength(length + p);
	}
	
	public void setLength(float length) {
		setSpringSize(length);
		setMaxSpringSize(length);
		if(rigid) {
			setMinSpringSize(length);
		}
		this.length = length;
	}
	
	public void setRigid(boolean rigid) {
		this.rigid = rigid;
	}
	
	public void setForwardSpeed(float speed) {
		expandSpeed = speed;
	}
	
	public void setReverseSpeed(float speed) {
		retractSpeed = speed;
	}
	
	public void setReverseCode(int c) {
		keyReverse = c;
	}
	
	public void setForwardCode(int c) {
		keyForward = c;
	}
	
	public void preStep(float invDT) {
		think();
		
		super.preStep(invDT);
	}

}
